package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.server.api.UserInfoApi;
import com.tanhua.server.interceptor.UserThreadLocal;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

@Service
public class UserInfoService {

    @Reference
    private UserInfoApi userInfoApi;

    @GetMapping
    public ResponseEntity<UserInfo> queryUserInfo(String paramUserId,String huanxinId) {

        Long userId = UserThreadLocal.getUserId();

        if (StringUtils.isNotBlank(paramUserId)) {
            userId = Long.valueOf(paramUserId);
        } else if (StringUtils.isNotBlank(huanxinId)) {
            userId = Long.valueOf(huanxinId);
        }

        UserInfo userInfo = userInfoApi.findByUserId(userId);

        return ResponseEntity.ok(userInfo);
    }

    @PutMapping
    public ResponseEntity<Void> updateUserInfo(UserInfo userInfo){
        userInfoApi.update(userInfo);
        return ResponseEntity.ok(null);
    }
}
